package com.knife.bbs.action;

import java.util.Date;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.MessageUtil;
import com.knife.bbs.domain.WebMessage;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IPageList;

public class WebMessageAction extends BBSAction {

	public WebMessageAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Page doInit(WebForm form, Module module) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page doWrite(WebForm form, Module module) {
		String toname = CommUtil.null2String(form.get("toname"));
		if (!"".equals(toname)) {
			form.addResult("toname", toname);
		}
		return module.findPage("/"+skin+"/","write");
	}

	public Page doListRecv(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pList = MessageUtil.queryRecv("toname='"
				+ this.getCurrentUser().getUsername() + "'", pageSize,
				currentPage);
		form.addResult("type", "收件箱");
		if (pList != null) {
			com.knife.util.CommUtil.saveIPageList2WebForm(pList, form);
		}
		return module.findPage("/"+skin+"/","list");
	}

	public Page doListSend(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pList = MessageUtil.querySend("fromname='"
				+ this.getCurrentUser().getUsername() + "'", pageSize,
				currentPage);
		form.addResult("type", "发件箱");
		if (pList != null) {
			com.knife.util.CommUtil.saveIPageList2WebForm(pList, form);
		}
		return module.findPage("/"+skin+"/","list");
	}

	public Page doListUnread(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pList = MessageUtil.queryUnRead("toname='"
				+ this.getCurrentUser().getUsername() + "'", pageSize,
				currentPage);
		if (pList != null) {
			form.addResult("type", "未读邮件");
			com.knife.util.CommUtil.saveIPageList2WebForm(pList, form);
		}
		
		return module.findPage("/"+skin+"/","list");
	}

	public Page doUnreadCount(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		form.addResult("unreadcount", WebMessage.UnReadNum(this
				.getCurrentUser().getUsername()));
		return module.findPage("/"+skin+"/","main");
	}

	public Page doRead(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "read", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String cid = CommUtil.null2String(form.get("cid"));
		if (!"".equals(cid)) {
			WebMessage wm = WebMessage.read(cid);
			form.addResult("title", wm.getTitle());
			form.addResult("fromname", wm.getFromname());
			form.addResult("message", wm.getMessage());
			form.addResult("date", wm.getSenddate());
			if (MessageUtil.isread(wm)) {
				MessageUtil.read(wm);
				if (wm.getAck().intValue() == 1) {
					MessageUtil.sendSystemMessage(WebMessage.ACKMSG, wm
							.getToname());
				}

			}
		}
		return module.findPage("/"+skin+"/","show");

	}

	public Page doSend(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "send", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String fromname = this.getCurrentUser().getUsername();
		String toname = CommUtil.null2String(form.get("toname"));
		String title = CommUtil.null2String(form.get("title"));
		String message = CommUtil.null2String(form.get("message"));
		String ACK = CommUtil.null2String(form.get("ACK"));
		WebMessage wm = new WebMessage();
		wm.setAck(new Integer(ACK));
		wm.setFromname(fromname);
		wm.setIsread(new Integer(0));
		wm.setMessage(message);
		wm.setSenddate(new Date());
		wm.setStatus(new Integer(0));
		wm.setTitle(title);
		wm.setToname(this.getCurrentUser().getUsername());
		wm.setType(new Integer(0));
		wm.setToname(toname);
		if (wm.save()) {
			form.addResult("msg", "发送成功!");
		}
		return this.doListSend(form, module);
	}

	public Page doSendUsers(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "send", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String fromname = this.getCurrentUser().getUsername();
		String toname = CommUtil.null2String(form.get("tonames"));
		String[] tonames = toname.split(";");
		boolean ret = false;
		for (int i = 0; i < tonames.length; i++) {
			String title = CommUtil.null2String(form.get("title"));
			String message = CommUtil.null2String(form.get("message"));
			String ACK = CommUtil.null2String(form.get("ACK"));
			WebMessage wm = new WebMessage();
			wm.setAck(new Integer(ACK));
			wm.setFromname(fromname);
			wm.setIsread(new Integer(0));
			wm.setMessage(message);
			wm.setSenddate(new Date());
			wm.setStatus(new Integer(0));
			wm.setTitle(title);
			wm.setToname(this.getCurrentUser().getUsername());
			wm.setType(new Integer(0));
			wm.setToname(tonames[i]);
			ret = wm.save();
		}
		if (ret) {
			form.addResult("msg", "发送成功!");
		}
		return this.doListSend(form, module);
	}

	public Page doDel(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "del", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String cid = CommUtil.null2String(form.get("cid"));
		if (!"".equals(cid)) {
			WebMessage wm = WebMessage.read(cid);
			if (MessageUtil.del(wm, this.getCurrentUser().getUsername())) {
				form.addResult("msg", "删除成功");
			}
		}
		return this.doListRecv(form, module);
	}

	public Page doMessage(WebForm form, Module module) {
		if (!BBSRights.checkRights(new WebMessage(), "msg", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		return module.findPage("/"+skin+"/","main");
	}

	private ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}

}
